﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using DataForest.DataModel;
using DataForest.ViewModel;

namespace DataForest.View.Converter
{
    class AttributToBrushConverter : Freezable, IValueConverter
    {

        private Workgroup group;

        public Workgroup Group
        {
            get { return (Workgroup)GetValue(GroupProperty); }
            set { SetValue(GroupProperty, value); }
        }

        public static readonly DependencyProperty GroupProperty =
            DependencyProperty.Register("Group", typeof(Workgroup), typeof(AttributToBrushConverter), new PropertyMetadata(GroupChanged));


        private static void GroupChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AttributToBrushConverter converter = d as AttributToBrushConverter;
            Workgroup tree = e.NewValue as Workgroup;

            if (tree == null || converter == null) return;

            converter.group = tree;
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is string && group != null) {
                string strValue = (string) value;
                return new SolidColorBrush(Group.GetAssociatedColor(strValue));
            }
            return Brushes.Black;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        protected override Freezable CreateInstanceCore()
        {
            return new AttributToBrushConverter();
        }
    }
}
